﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AudioCore.Biz;
using AudioCore.Entity;
using AudioCore.Common;

namespace AudioBook.AudioManagement.UC
{
    public partial class CategoryUC : System.Web.UI.UserControl
    {
        #region Public properties

        public event EventHandler SelectedIndexChanged;

        public int ModuleID
        {
            get
            {
                return int.Parse(hidModuleID.Value);
            }
            set
            {
                hidModuleID.Value = value.ToString();
            }
        }

        public int SelectedCategoryID
        {
            get
            {
                if (dropCategory.Items.Count != 0)
                {
                    return int.Parse(dropCategory.SelectedValue);
                }
                else
                {
                    return 0;
                }
            }
        }

        public bool AutoPostBack
        {
            get
            {
                return dropCategory.AutoPostBack;
            }
            set
            {
                dropCategory.AutoPostBack = value;
            }
        }

        public bool AllowToValidate
        {
            get
            {
                return reqCategory.Enabled;
            }
            set
            {
                reqCategory.Enabled = value;
            }
        }

        public Unit Width
        {
            get
            {
                return dropCategory.Width;
            }
            set
            {
                dropCategory.Width = value;
            }
        }

        #endregion

        #region Fields & constants

        CategoryBiz bizCategory = new CategoryBiz();
        List<CategoryEntity> lstCategory;

        #endregion Fields & constants

        #region Private methods

        private void LoadCategory()
        {
            dropCategory.Items.Clear();

            lstCategory = bizCategory.GetAllCategory();
            lstCategory.Sort(delegate(CategoryEntity enCat1, CategoryEntity enCat2) { return enCat2.SortOrder.CompareTo(enCat1.SortOrder); });
            ProcessCategory(0, string.Empty);
        }

        private void ProcessCategory(int parentID, string separate)
        {
            List<CategoryEntity> lstSubCat = lstCategory.Where(item => item.ParentID == parentID).ToList();
            foreach (CategoryEntity enCategory in lstSubCat)
            {
                ListItem dropItem = new ListItem(separate + enCategory.CategoryName, enCategory.CategoryID.ToString());
                dropCategory.Items.Add(dropItem);

                ProcessCategory(enCategory.CategoryID, separate + "...");
            }
        }

        #endregion

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategory();
            }
        }

        protected void dropCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null)
            {
                SelectedIndexChanged(sender, e);
            }
        }
        #endregion

    }
}